﻿Imports System.Data.SqlClient
Public Class EmployeeEdotor
    Dim cmd As New SqlCommand
    Private Sub EmployeeEdotor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        condb()
        txtid.Hide()
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub btnaddeditor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnaddeditor.Click
        cmd.Connection = cn
        cmd.CommandText = "select empcode from tblemployee where empcode='" & txtcode.Text & "'"
        Dim code = cmd.ExecuteScalar
        If txtcode.Text = "" Then
            MessageBox.Show("Please reguire field employee code", "Field info", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
            txtcode.Focus()
            Exit Sub
        ElseIf txtcode.Text = code Then
            MessageBox.Show("This employee code is already exist", "Field info", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
            Exit Sub
        End If
        If txtname.Text = "" Then
            MessageBox.Show("Please reguire field employee name", "Field info", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
            Exit Sub
        End If
        cmd.Connection = cn
        cmd.CommandText = "insert into tblemployee values(N'" & txtcode.Text & "',N'" & txtname.Text & "',N'" & txtgender.Text & "',N'" & txtpob.Text & "','" & txtdob.Value & "',N'" & txtphon.Text & "',N'" & txtsalary.Text & "',N'" & txtposition.Text & "',N'" & txtdescription.Text & "')"
        cmd.ExecuteNonQuery()
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click
        If txtcode.Text = "" Then
            MessageBox.Show("Please reguire field employee code", "Field info", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
            txtcode.Focus()
            Exit Sub
        End If
        If txtname.Text = "" Then
            MessageBox.Show("Please reguire field employee name", "Field info", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
            Exit Sub
        End If
        cmd.Connection = cn
        cmd.CommandText = "update Tblemployee set Empcode=N'" & txtcode.Text & "', Name=N'" & txtname.Text & "',sex=N'" & txtgender.Text & "',POB=N'" & txtpob.Text & "',DOB='" & txtdob.Value & "',Phone=N'" & txtphon.Text & "',Salary=N'" & txtsalary.Text & "',position=N'" & txtposition.Text & "',description='" & txtdescription.Text & "' where EmpID='" & txtid.Text & "';"
        cmd.ExecuteNonQuery()
        MessageBox.Show("Update Succes", "", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
        Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub
End Class